package com.zoz.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zoz.service.CommonCodeFactoryEngineService;
import com.zoz.vo.EngineModelVo;
import com.zoz.vo.EngineVo;

/**
 * 公共生成文件
 *@author zhuhg
 *@date 2020年6月5日 下午3:31:57
 */
@Controller
@RequestMapping(value = {"/code/factory/service/v1/factoryEngine/"}, produces = {"application/json;charset=utf-8"})
public class CommonCodeFactoryEngineController {
	private static final Logger logger = LoggerFactory.getLogger(ResourceLoaderController.class);
	@Autowired
	private CommonCodeFactoryEngineService commonCodeFactoryEngineService;
	 
	@PostMapping(value = {"/readEngineToJavaFile"}, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String readEngineToJavaFile(EngineVo vo) {
		String param = JSON.toJSONString(vo);
		String result = "";
		logger.info("读取本地文件参数{}",param);
		try {
			result = commonCodeFactoryEngineService.readEngineToJavaFile(vo);
		} catch (Exception e) {
			logger.error("读取本地文件失败{}",param,e);
		}
        // 查询结果返回前端
        return result;
    }
	@PostMapping(value = {"/downloadFile"}, produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public void downloadFile(HttpServletRequest request, HttpServletResponse response,EngineVo vo) {
		String param = JSON.toJSONString(vo);
		logger.info("生成本地文件参数{}",param);
		try {
			 commonCodeFactoryEngineService.downloadFile(request, response, vo);
		} catch (Exception e) {
			logger.error("生成本地文件失败{}",param,e);
		}
	}
	@PostMapping(value = {"/downloadMarryFile"}, produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public void downloadMarryFile(HttpServletRequest request, HttpServletResponse response,EngineModelVo vo) {
		String param = JSON.toJSONString(vo);
		logger.info("生成本地文件参数{}",param);
		try {
			commonCodeFactoryEngineService.downloadMarryFile(request, response, vo);
		} catch (Exception e) {
			logger.error("生成本地文件失败{}",param,e);
		}
	}
}
